@model ThridGroup.ERP.DTO.ProductionSystem.ProductionWasteOrderDTO
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑生产废料单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div style="padding: 20px;">
        <form class="layui-form" lay-filter="wasteOrderForm">
            <input type="hidden" name="id" value="@Model.Id">
            <div class="layui-form-item">
                <label class="layui-form-label">废料单编号</label>
                <div class="layui-input-block">
                    <input type="text" name="wasteOrderCode" required lay-verify="required" placeholder="自动生成" autocomplete="off" class="layui-input" value="@Model.WasteOrderCode" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color:red;">*</span>生产工单</label>
                <div class="layui-input-block">
                    <div class="layui-input-group">
                        <input type="text" name="productionWorkOrder" lay-verify="required" placeholder="请选择生产工单" autocomplete="off" class="layui-input" value="@Model.ProductionWorkOrder">
                        <div class="layui-input-group-append">
                            <button type="button" class="layui-btn layui-btn-primary" id="selectWorkOrderBtn">
                                <i class="layui-icon layui-icon-search"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">废料单名称</label>
                <div class="layui-input-block">
                    <input type="text" name="wasteOrderName" required lay-verify="required" placeholder="请输入废料单名称" autocomplete="off" class="layui-input" value="@Model.WasteOrderName">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">废料日期</label>
                <div class="layui-input-block">
                    <input type="text" name="wasteDate" id="wasteDate" placeholder="请选择废料日期" autocomplete="off" class="layui-input" value="@(Model.Id > 0 ? Model.WasteDate.ToString("yyyy-MM-dd HH:mm:ss") : "")">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单据状态</label>
                <div class="layui-input-block">
                    <select name="orderStatus" lay-verify="required">
                        <option value="草稿" selected="@(Model.OrderStatus == "草稿")">草稿</option>
                        <option value="已完成" selected="@(Model.OrderStatus == "已完成")">已完成</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">仓库名称</label>
                <div class="layui-input-block">
                    <input type="text" name="warehouseName" placeholder="请输入仓库名称" autocomplete="off" class="layui-input" value="@Model.WarehouseName">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: center;">
                <button class="layui-btn" lay-submit lay-filter="submitForm">确定</button>
                <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
            </div>
        </form>
    </div>
    <script>
        layui.use(['form', 'layer', 'laydate', 'table'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var table = layui.table;
            var $ = layui.$;

            laydate.render({ elem: '#wasteDate', type: 'datetime' });

            // 新增模式下自动生成编号
            if (!$('input[name="id"]').val() || $('input[name="id"]').val() == '0') {
                var now = new Date();
                var year = now.getFullYear();
                var month = (now.getMonth() + 1).toString().padStart(2, '0');
                var day = now.getDate().toString().padStart(2, '0');
                var hours = now.getHours().toString().padStart(2, '0');
                var minutes = now.getMinutes().toString().padStart(2, '0');
                var seconds = now.getSeconds().toString().padStart(2, '0');
                var newCode = `WL${year}${month}${day}${hours}${minutes}${seconds}`;
                $('input[name="wasteOrderCode"]').val(newCode);
                $('input[name="wasteDate"]').val(layui.util.toDateString(now, 'yyyy-MM-dd HH:mm:ss'));
            }

            form.render(null, 'wasteOrderForm');

            // 生产工单选择按钮弹窗逻辑
            $('#selectWorkOrderBtn').on('click', function () {
                layer.open({
                    type: 1,
                    title: '选择生产工单',
                    area: ['800px', '500px'],
                    content: '<div style="padding: 10px;"><table id="workOrderTable" lay-filter="workOrderTable"></table></div>',
                    success: function (layer_work_order, index_work_order) {
                        table.render({
                            elem: '#workOrderTable',
                            url: '/Prouduction/ShowProdction',
                            page: true,
                            request: {
                                pageName: 'pageIndex',
                                limitName: 'pageSize'
                            },
                            cols: [[
                                { field: 'orderCode', title: '工单编号', width: 200 },
                                { field: 'orderName', title: '工单名称', width: 200 },
                                { field: 'productionName', title: '产品名称' },
                                { field: 'orderCount', title: '工单数量', width: 100 }
                            ]],
                            parseData: function (res) {
                                return {
                                    "code": res.code,
                                    "msg": res.msg,
                                    "count": res.count,
                                    "data": res.data
                                };
                            }
                        });
                        // 监听行单击事件
                        table.on('row(workOrderTable)', function (obj) {
                            var selectedData = obj.data;
                            // 填充表单
                            form.val('wasteOrderForm', {
                                'productionWorkOrder': selectedData.orderCode,
                                'wasteOrderName': selectedData.orderName
                            });
                            // 关闭选择窗口
                            layer.close(index_work_order);
                        });
                    }
                });
            });

            form.on('submit(submitForm)', function (data) {
                var submitData = data.field;
                var isEdit = submitData.id && submitData.id > 0;
                var url = isEdit ? '/ProductionWasteOrder/UpdateProductionWasteOrder' : '/ProductionWasteOrder/AddProductionWasteOrder';
                var method = isEdit ? 'PUT' : 'POST';
                if (!isEdit) submitData.id = 0;
                if (submitData.wasteDate) {
                    submitData.wasteDate = new Date(submitData.wasteDate).toISOString();
                }
                $.ajax({
                    url: url,
                    type: method,
                    contentType: 'application/json',
                    data: JSON.stringify(submitData),
                    success: function (res) {
                        layer.msg('保存成功', { icon: 1, time: 1000 }, function(){
                            window.location.href = '/ProductionWasteOrder/ProductionWasteOrderView';
                        });
                    },
                    error: function () {
                        layer.msg('保存失败');
                    }
                });
                return false;
            });
            $('#btnCancel').on('click', function() {
                window.location.href = '/ProductionWasteOrder/ProductionWasteOrderView';
            });
        });
    </script>
</body>
</html> 